package com.homegoods.discover.presenter;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.homegoods.area.model.SubModelList;
import com.homegoods.classify.model.Constant;
import com.homegoods.classify.model.VideoConstant;
import com.homegoods.discover.DiscoverApiUtils;
import com.homegoods.discover.model.BestCollectModel;
import com.homegoods.discover.model.VideoModel;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 * @ClassName: VideoPresenter
 * @date 2018/9/10
 * @description
 */
public class VideoPresenter extends AbstractRefreshPresenter<RefreshingView> {
    int[] videoIndexs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    public VideoPresenter(RefreshingView mView) {
        super(mView);
    }

    /**
     * 获取第一帧视频
     *
     * @param videoUrl
     * @return
     */
    public static Bitmap getNetVideoBitmap(String videoUrl) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根据url获取第一帧
            retriever.setDataSource(videoUrl, new HashMap());
            // 获取本地视频的第一帧
            // retriever.setDataSource("/sdcard/03.mp4");
            // 获得第一帧图片
            bitmap = retriever.getFrameAtTime();
            // 视频第一帧的压缩
            /* FileOutputStream outStream = null;
            outStream = new FileOutputStream(new File(getExternalCacheDir().getAbsolutePath() + "/" + "视频" + ".jpg"));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 10, outStream);
            outStream.close();*/
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }

    @Override
    protected void fetchData(boolean isLoadMore) {
        DiscoverApiUtils.getDiscoverList(new Consumer<SubModelList>() {
            @Override
            public void accept(SubModelList subModelList) throws Exception {
                List<VideoModel> list = JSON.parseArray(subModelList.getItems(), VideoModel.class);
                VideoPresenter.this.parserData(isLoadMore, list);
            }
        }, throwable -> {
            throwable.printStackTrace();
        });
        mView.setRefreshing(false);
    }
}
